package com.service.uai.engine.comm;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

import com.bpms.common.CommonUtil;
import com.bpms.common.Param;
import com.bpms.eai.CommonServer;
import com.bpms.eai.IAsynComm;
import com.bpms.eai.PackTool;
import com.service.uai.engine.pack.ATMServerPack;

public class ATMServer extends PackTool implements IAsynComm {
	private static ATMServer instance = null;

	public static ATMServer I() {
		if (instance == null) {
			instance = new ATMServer();
		}
		return instance;
	}

	public ATMServer() {
		try {
			int port = Integer.parseInt(Param.getValue("UAI", "atm_server_port"));
			int timeout = Integer.parseInt(Param.getValue("UAI", "timeout"));
			CommonServer cs = new CommonServer(this, new ATMServerPack(), port, timeout*1000);
			cs.startServer();
			CommonUtil.debug("ATMServer is start at port:" + port);
		} catch (Exception e) {
			CommonUtil.debug("ATMServer start exception", e);
		}
	}

	public byte[] readBuffer(Socket socket) {
		try {
			// 本报文长度
			byte[] bbodylen = new byte[8];
			int len = socket.getInputStream().read(bbodylen, 0, 8);
			if (len < 8) {
				CommonUtil.debug("atmserver the send bodylen is short " + new String(bbodylen));
				return null;
			}
			String sbodylen = new String(bbodylen);
			int ibodylen = Integer.parseInt(sbodylen);
			CommonUtil.debug("atmserver receive bodylens is ：" + sbodylen);
			// 流水号
			byte[] bseqid = new byte[8];
			len = socket.getInputStream().read(bseqid, 0, 8);
			if (len < 8) {
				CommonUtil.debug("atmserver the send seqid is short " + new String(bseqid));
				return null;
			}
			String sseqid = new String(bseqid);
			CommonUtil.debug("atmserver receive seqid is ：" + sseqid);
			// 交易报文总长度
			byte[] bwholelen = new byte[8];
			len = socket.getInputStream().read(bwholelen, 0, 8);
			if (len < 8) {
				CommonUtil.debug("atmserver the send wholelen is short " + new String(bwholelen));
				return null;
			}
			String swholelen = new String(bwholelen);
			int iwholelen = Integer.parseInt(swholelen);
			if (iwholelen == (ibodylen - 24)) {
				CommonUtil.debug("atmserver this trans is complete ：" + swholelen);
			} else {
				CommonUtil.debug("atmserver this trans is not complete ：" + swholelen);
			}
			// 交易码
			byte[] head = new byte[4];
			int headlen;
			headlen = socket.getInputStream().read(head, 0, 4);
			if (headlen < 4) {
				CommonUtil.debug("atmserver the head len is short " + new String(head, "GBK"));
				return null;
			}
			String shead = new String(head);
			CommonUtil.debug("atmserver the head :" + shead);
			// 结果码
			byte[] result = new byte[4];
			len = socket.getInputStream().read(result, 0, 4);
			if (len < 4) {
				CommonUtil.debug("atmserver the result len is short " + new String(result, "GBK"));
				return null;
			}
			String sresult = new String(result);
			CommonUtil.debug("atmserver the sresult 1:" + sresult);

			if ("0000".equals(sresult)) {
				CommonUtil.debug("enter:");
				byte[] body1 = new byte[ibodylen - 24 + 1536];
				byte[] buffer = new byte[4096];
				//byte[] body1 = new byte[ibodylen - 24 + 1024000];
				//byte[] buffer = new byte[1024000];
				int size = buffer.length;
				int cur = 0;
				int totalSize = 0;
				// while(size!=-1 && size>=buffer.length){
				// size=socket.getInputStream().read(buffer,0,size);
				// totalSize+=size;
				// if(size==-1)
				// {
				//
				// }
				// else{
				// this.memcpy(body1, (cur*buffer.length), buffer, 0, size);
				//
				// }
				// cur++;
				// }
				while (size != -1 && totalSize < iwholelen) {
					// CommonUtil.debug("totalSize1:"+totalSize+" size:"+size+" iwholelen:"+iwholelen);
					size = socket.getInputStream().read(buffer, 0, size);

					// CommonUtil.debug("totalSize2:"+totalSize+" size:"+size+" iwholelen:"+iwholelen);
					if (size == -1) {

					} else {
						// this.memcpy(body1, (cur*buffer.length), buffer, 0,
						// size);
						this.memcpy(body1, totalSize, buffer, 0, size);

					}
					totalSize += size;
					cur++;
				}
				// len =socket.getInputStream().read(body1,0,ibodylen-24);
				// if (len < (ibodylen-24)) {
				// CommonUtil.debug("atmserver read length<"+(ibodylen-24)+" len="+len);
				// return null;
				// }
				CommonUtil.debug("body1.len:" + body1.length + " totalSize:" + totalSize + " atmserver read body1");

				// return memmerge(memmerge(bbodylen,head),body1);
				InetAddress ia = socket.getInetAddress();
				String atmip = ia.getHostAddress();
				atmip = addleftspace(atmip, 15);
				//return memmerge(atmip.getBytes(), memmerge(memmerge(bbodylen, head), body1));
				return memmerge(bbodylen, memmerge(memmerge(memmerge(memmerge(bseqid, bwholelen),head),result), body1));
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			CommonUtil.debug("atmserver.readBuffer exception", e);

		}
		return null;
	}

	public void sendBuffer(byte[] buffer, Socket socket) {
		try {
			OutputStream outputStream = socket.getOutputStream();
			OutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(outputStream));
			CommonUtil.debug("atmserver.sendBuffer(" + new String(buffer,"gb2312").length() + ")" + new String(buffer,"gb2312"));
			dataOutputStream.write(buffer);
			dataOutputStream.flush();
		} catch (Exception e) {
			CommonUtil.debug("atmserver.sendBuffer exception", e);
			//todo:没有处理发送消息失败该如何回滚
		}

	}

}
